package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.PartnerDocument;
import org.hgvc.webapp.ets.bo.PartnerProductType;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class AdminPartnerProductTypeMasterBean extends AbstractBean{

	public static final String BEAN_NAME="adminPartnerProductTypeMasterBean";
	private PartnerService service;
	private PartnerProductType type;
	private List<PartnerProductType> typeList;
	
	public static final String NAV_EDIT_ACTION="adminPartnerProductTypeEdit";
	public static final String NAV_CREATE_ACTION="adminPartnerProductTypeCreate";
	public static final String NAV_LIST_ACTION="adminPartnerProductTypeList";
	public PartnerProductType getType() {
		return type;
	}
	public void setType(PartnerProductType type) {
		this.type = type;
	}
	public void setService(PartnerService service) {
		this.service = service;
	}
	
	/*------------------------------------------------------------------*/
	public List<PartnerProductType> getTypeList() {
		try{
			typeList = service.getPartnerProductTypeList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return typeList;
	}

	public String navCreateAction(){
		type = new PartnerProductType();
		return NAV_CREATE_ACTION;
	}
	
	public String createAction(){
		try{
			service.createPartnerProductType(type, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_CREATE_ACTION;
	}

	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("typeId");
		String id = (String)param.getValue();

		for(PartnerProductType t:typeList){
			if(t.getPartnerProductTypeId().equalsIgnoreCase(id)){
				type = t;
				break;
			}
		}
	}

	public String navEditAction(){
		return NAV_EDIT_ACTION;
	}

	public String editAction(){
		try{
			service.updatePartnerProductType(type, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_EDIT_ACTION;
	}

	public String deleteAction(){
		try{
			service.deletePartnerProductType(typeList, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST_ACTION;
	}
	
	public String navListAction(){
		return NAV_LIST_ACTION;
	}
}
